package com.dengqi.mymodule.activity.network.activity;

import android.os.Bundle;
import android.view.View;

import com.dengqi.mymodule.R;
import com.dengqi.mymodule.activity.network.adapter.ListAdapter;
import com.dengqi.mymodule.activity.network.bean.MemberBean;
import com.dengqi.mymodule.activity.network.module.MemberContract;
import com.dengqi.mymodule.activity.network.module.MemberPresenter;
import com.dengqi.mymodule.activity.network.view.GlobalLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.module.retrofit.utils.ToastUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * @Author: dengqi
 * @CreateDate: 2020/10/19 17:25
 * @Description:
 */
public class MemberListActivity extends RxAppCompatActivity implements MemberContract.MemberListView {

    private XRecyclerView recyclerView;
    private ListAdapter adapter;
    private MemberPresenter memberPresenter;
    private String keyword= ""; //可以用editText代替String
    private GlobalLayout globalLayout;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list);

        globalLayout = findViewById(R.id.emptyView);
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ListAdapter();
        recyclerView.setAdapter(adapter);
        globalLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast("刷新");
            }
        });
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                memberPresenter.getMemberList(MemberListActivity.this, globalLayout, false);
            }

            @Override
            public void onLoadMore() {
                memberPresenter.getMemberList(MemberListActivity.this, globalLayout, true);
            }
        });

        memberPresenter = new MemberPresenter(this);
        memberPresenter.getMemberList(this, globalLayout, false);
    }

    public void onBtn1(View view){
        keyword = "15993012675";
        memberPresenter.getMemberList(this, globalLayout, false);
    }

    public void onBtn2(View view){
        keyword = "";
        memberPresenter.getMemberList(this, globalLayout, false);
    }

    @Override
    public String getBusId() {
        return "1";
    }

    @Override
    public String getShopId() {
        return "1";
    }

    @Override
    public String getAscShopId() {
        return "";
    }

    @Override
    public String getKeyword() {
        //可以用editText.getText().toSrting()代替keyword
        return keyword;
    }

    @Override
    public String getLevel() {
        return "";
    }

    @Override
    public String getSource_() {
        return "";
    }

    @Override
    public String getTag_() {
        return "";
    }

    @Override
    public String getConsumeMonth() {
        return "";
    }

    @Override
    public String getConsumeCount() {
        return "";
    }

    @Override
    public String getBirthStart() {
        return "";
    }

    @Override
    public String getBirthEnd() {
        return "";
    }

    @Override
    public void onTotalSize(int totalSize) {
        if(adapter.getItemCount() >= totalSize){
            recyclerView.setLoadingMoreEnabled(false);
        }else{
            recyclerView.setLoadingMoreEnabled(true);
        }
    }

    @Override
    public void onMemberList(List<MemberBean.ListBean> result, boolean isLoadMore) {
        if(isLoadMore){
            recyclerView.loadMoreComplete();
            adapter.addDatas(result);
        }else{
            recyclerView.refreshComplete();
            adapter.setDatas(result);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onResultError(boolean isLoadMore) {
        if(isLoadMore){
            recyclerView.loadMoreComplete();
        }else{
            recyclerView.refreshComplete();
        }
    }
}
